package gov.decea.projects.sgq.controllers;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import gov.decea.projects.sgq.api.MainService;
import gov.decea.projects.sgq.core.exception.CoreException;
import gov.decea.projects.sgq.forms.AerodromoFormBean;
import gov.decea.projects.sgq.model.Aerodromo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 *
 * @author dctp
 */
@Controller
@RequestMapping("/aerodromos")
public class AerodromosController {

    private final MainService mainService;

    @Autowired
    public AerodromosController(MainService mainService) {
        this.mainService = mainService;
    }

    @RequestMapping({"/", "/index", ""})
    public String index(Model model, WebRequest request) {

        List<Map<Object, Object>> dataList = new ArrayList<Map<Object, Object>>();

        List<Aerodromo> aerodromos = mainService.findAllAerodromos();

        for (Aerodromo aerodromo : aerodromos) {
            Map<Object, Object> item = new HashMap<Object, Object>();
            item.put("id", aerodromo.getId());
            item.put("sigla", aerodromo.getSigla());
            item.put("descricao", aerodromo.getDescricao());
            
            dataList.add(item);
        }

        model.addAttribute("total", dataList.size());
        model.addAttribute("dataList", dataList);

        return "aerodromos";
    }

    @RequestMapping("/new")
    public String newAerodromo(Model model) {

        AerodromoFormBean form = new AerodromoFormBean();
        model.addAttribute(form);
        return "aerodromos/form";
    }

    @RequestMapping("/edit")
    public String editAerodromo(Model model, WebRequest request) {
        AerodromoFormBean form = new AerodromoFormBean();
        String idParam = request.getParameter("id");
        if (idParam == null) {
            return "redirect:index";
        }
        Long id = Long.parseLong(idParam);

        Aerodromo aerodromo = mainService.findAerodromo(id);
        form.setId(aerodromo.getId());
        form.setSigla(aerodromo.getSigla());
        form.setDescricao(aerodromo.getDescricao());
        form.setOrganizacaoId(4L);
        // PREENCHER FORMULÁRIO COM ID INCLUSIVE
        model.addAttribute(form);
        return "aerodromos/form";
    }

    @RequestMapping("/save")
    public String saveAerodromo(Model model, @Valid AerodromoFormBean aerodromoFormBean,
            BindingResult bindingResult, WebRequest request) {

        if (bindingResult.hasErrors()) {
            return "aerodromos/form";
        }

        Aerodromo aerodromo = new Aerodromo();
        aerodromo.setId(aerodromoFormBean.getId());
        aerodromo.setSigla(aerodromoFormBean.getSigla());
        aerodromo.setDescricao(aerodromoFormBean.getDescricao());
        aerodromo.setOrganizacaoId(4L);

        try {
            if (aerodromo.getId() <= 0) {
                Long id = mainService.createAerodromo(aerodromo);
                aerodromo.setId(id);
                model.addAttribute("successMessage", "Aeródromo criado com sucesso!");
            } else {
                mainService.updateAerodromo(aerodromo);
                model.addAttribute("successMessage", "Aeródromo atualizado com sucesso!");
            }

        } catch (CoreException ex) {
            model.addAttribute("errorMessage", ex.getMessage());
            return "aerodromos/form";
        }

       

        model.addAttribute("data", aerodromo);

        return "aerodromos/detail";

    }

    @RequestMapping("/detail")
    public String detailAerodromo(Model model, WebRequest request) {
       
        String idParam = request.getParameter("id");
        if (idParam == null) {
            return "redirect:index";
        }
        Long id = Long.parseLong(idParam);
        Aerodromo aerodromo = mainService.findAerodromo(id);



        model.addAttribute("data", aerodromo);
        return "aerodromos/detail";
    }

    @RequestMapping("/remove")
    public String removeAerodromo(Model model, WebRequest request,
            final RedirectAttributes redirectAttributes) {
        Map<Object, Object> data = new HashMap<Object, Object>();
        String idParam = request.getParameter("id");
        if (idParam == null) {
            return "redirect:index";
        }
        Long id = Long.parseLong(idParam);

        try {
            mainService.removeAerodromo(id);
            redirectAttributes.addFlashAttribute("successMessage", "Aeródromo foi removido com sucesso!");
            
        } catch (CoreException ex) {
            redirectAttributes.addFlashAttribute("errorMessage", ex.getMessage());
            return "redirect:index";
        }

        return "redirect:index";
    }
}
